<?php
/**
 * Created by PhpStorm.
 * User: syzhou
 * Date: 2020-11-12
 * Time: 17:19
 */

// 该脚本将待迁移项目的所有上传文件的路径抽取出来，保存到一个文件

require_once __DIR__ . '/common.php';

/**
 * @var PDO
 */
global $srcPDO;

/**
 * @var PDO
 */
global $dstPDO;

$srcProjectIds = getSrcProjectIds();
$selectSrcProjectFormDataStmt = $srcPDO->prepare("select id, form_data from application_form_data where id = ?");
$uploadedFiles = [];
foreach ($srcProjectIds as $srcProjectId) {
    $selectSrcProjectFormDataStmt->execute([$srcProjectId]);
    $srcProjectFormData = $selectSrcProjectFormDataStmt->fetch(PDO::FETCH_ASSOC);
    $selectSrcProjectFormDataStmt->closeCursor();
    $formDataStr = $srcProjectFormData['form_data'];
    $formData = json_decode($formDataStr, true);
    $documentPhoto = $formData['basic']['document_photo'];
    if (!empty($documentPhoto)) {
        $uploadedFiles[] = $documentPhoto;
    }
    foreach ($formData['attachments'] as $path) {
        if (!empty($path)) {
            $uploadedFiles[] = $path;
        }
    }
}

$uploadedFilePathsSavePath = __DIR__ . '/uploaded_file_paths.txt';
$fh = fopen($uploadedFilePathsSavePath, 'w');
foreach ($uploadedFiles as $uploadedFile) {
    fwrite($fh, "$uploadedFile\n");
}
fclose($fh);